<template>
	<view class="addzh">
		<headertop :title="title" str1="oridei" :back1="back1"></headertop>
		<image src="https://jingwei.xunfuzhifuzaixian.com/static/images/Rectangle 2@2x.png" class="addzhback" mode="">
		</image>
		<view :style="{'margin-top':statusBarHeight+'px','height':barHeight+'px'}"></view>
		<view class="contents">
			<view class="itembox">
				<view class="labletitle">
					类型：
				</view>
				<view class="rightxz">
					银行卡
				</view>
			</view>
			<view class="itembox">
				<view class="labletitle">
					账号名称：
				</view>
				<view class="rightxz">
					<input type="text" v-model="formData['bankname']" placeholder="请输入账号名称" />
				</view>
			</view>
			<view class="itembox">
				<view class="labletitle">
					收款人：
				</view>
				<view class="rightxz">
					<input type="text" v-model="formData['username']" placeholder="请输入收款人名称" />
				</view>
			</view>
			<view class="itembox">
				<view class="labletitle">
					打款账号：
				</view>
				<view class="rightxz">
					<input type="number" v-model="formData['cardnum']" placeholder="请输入打款账号" />
				</view>
			</view>
		</view>
		<view class="fixedbox">
			<view class="btn" @click="qd">
				{{title == '编辑账户'?'保存':'确认添加'}}
			</view>
		</view>
	</view>
</template>
<script setup>
	import headertop from '@/components/header.vue'
	import {
		$api
	} from '../../ulm';
	import {
		onPageScroll,
		onLoad,
		onShow
	} from "@dcloudio/uni-app";
	import {
		ref,
		onMounted,
		reactive,
	} from 'vue'
	const back1 = ref('')
	const barHeight = ref('')
	const statusBarHeight = ref('')
	const title = ref('')
	const formData = reactive({
		id: '',
		bankname: '',
		username: '',
		cardnum: ''
	})
	onLoad((opt) => {
		console.log(opt, 'optoptoptoptoptopt');
		if (opt.id) {
			title.value = '编辑账户'
			formData.id = opt.id
			getinof(opt.id)
		} else {
			title.value = '添加账户'
		}
	})
	onPageScroll((e) => {
		if (e.scrollTop > 100) {
			back1.value = '#CAEEFF'
		} else {
			back1.value = ''
		}
	})
	onMounted(() => {
		statusBarHeight.value = uni.getSystemInfoSync().statusBarHeight
		const {
			top,
			height
		} = wx.getMenuButtonBoundingClientRect();
		barHeight.value = height ? height + (top - statusBarHeight.value) * 2 : 38;
	})
	async function getinof(id) {
		const res = await $api.banklist({
			page: 1,
			limit: 100
		})
		if (res.code == 1) {
			var ind = res.data.list.findIndex(item => item.id == id)
			formData['bankname'] = res.data.list[ind]['bankname']
			formData['username'] = res.data.list[ind]['username']
			formData['cardnum'] = res.data.list[ind]['cardnum']
		}
	}
	async function qd() {
		for (var i in formData) {
			if (i != 'id') {
				if (!formData[i]) {
					uni.showToast({
						title: '请补全信息后提交',
						icon: 'none'
					})
					return
				}
			}
		}
		const bankCardRegex = /^[1-9]\d{15,18}$/;
		if (bankCardRegex.test(formData.cardnum)) {} else {
			uni.showToast({
				title: '银行卡号格式错误',
				icon: 'none'
			})
			return
		}
		const res = await $api.bankadd(formData)
		if (res.code == 1) {
			uni.navigateBack()
			var title = ''
			if (formData.id) {
				title = '编辑成功'
			} else {
				title = '添加成功'
			}
			setTimeout(() => {
				uni.showToast({
					title,
					icon: 'none'
				})
			}, 500)
		} else {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			})
		}
	}
</script>

<style lang="scss">
	page {
		background: #F7F8FA;
	}

	.addzh {
		width: 100%;
		overflow: hidden;
		position: relative;

		.addzhback {
			width: 100%;
			position: absolute;
			z-index: -1;
			height: 308rpx;
		}

		.fixedbox {
			height: 186rpx;
			background: #FFFFFF;
			box-shadow: 0rpx -4rpx 10rpx 0rpx rgba(0, 0, 0, 0.06);
			width: 100%;
			position: fixed;
			bottom: 0%;

			.btn {
				width: 686rpx;
				height: 82rpx;
				background: #13A2E6;
				border-radius: 12rpx 12rpx 12rpx 12rpx;
				text-align: center;
				line-height: 82rpx;
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #FFFFFF;
				margin: auto;
				margin-top: 24rpx;
			}
		}

		.contents {
			width: 686rpx;
			margin: auto;
			margin-top: 36rpx;
			overflow: hidden;

			.itembox {
				width: 100%;
				height: 92rpx;
				background: #FFFFFF;
				border-radius: 12rpx 12rpx 12rpx 12rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 24rpx;

				.labletitle {
					width: 250rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 32rpx;
					color: #1D2129;
					white-space: nowrap;
					margin-left: 28rpx;
				}

				.rightxz {
					display: flex;
					align-items: center;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 32rpx;
					color: #1D2129;
					margin-right: 28rpx;

					input {
						text-align: right;
					}
				}
			}
		}
	}
</style>